

(function(){
	function autoComplatePlaceHandler(_place){
    	var places = new Array();
    	places[0] = _place;
    	//addSelectedCityMarker(_place);
    	var oneLocationContent = createLocationContentForInfoWindow(_place);
    	var container  = addLocationToLocationContainer(oneLocationContent);
        var html = $("<div>").append(container).html();
		var infoWindowContent = html;
		$gmap.openInfoWindowOnMap(infoWindowContent,_place.geometry.location,infoWindowContentEventHandler,places);	
    
    }
    function addCityMarkerHandler(response){
   	  if(response.success == true){
   	  	  alert(response.message);
   	  }
   	  else if(response.success==false){
   		  alert(response.message);
   	  }
   }
    function deleteCityMarkerHandler(response){
    	
    }
    function addLocationToLocationContainer(onelocations){
    	var locationContatiner = $("<div>").attr({'class':'locationContainer','id':'locationContainer'});
    	for(i = 0; i < onelocations.length; i++){
	    	locationContatiner.append(onelocations[i]);
	    }
	    return locationContatiner;
    }
    function createLocationContentForInfoWindow(content){
        var divLocationContainer = $("<div>").attr({'id':'location','class':"oneLocation"});
        var divCityName = "Add This Place";
        var checkBoxContainer = $("<div>").attr({'id':'checkBoxContainer','class':'checkBoxContainerForPlace'})
        var beenSelectCheckBox = $("<input>").attr({'type':"checkBox",
                                                    'name':"beenSelect:",
                                                    'value':"beenSelect",
                                                    'class':"selectCheckBoxForPlace"
                                                   });
        
        checkBoxContainer.append(beenSelectCheckBox);
        var placeTypesSelectBox = $("#relatedPlacesSelects").clone();
        placeTypesSelectBox.css("display","block").attr({'id':"addNewPlaceTypeSelectBox"});
        divLocationContainer.append(checkBoxContainer).append(divCityName).append(placeTypesSelectBox);
        return  divLocationContainer;                                           
  	}
    function infoWindowContentEventHandler(places){
 
   		$("#locationContainer").find(".selectCheckBoxForPlace").each(function(index) {
			 var thisSelectBox = $(this);
			 $(this).click(function() {
				 var name = thisSelectBox.attr("name");
				 var id = thisSelectBox.attr("id");
				 //currentSelectedPlaceId =id;
				 if(thisSelectBox.attr("checked")){
			    	  if(places[0].requestType=="myPlace"){
			      		addSelectedCityMarkerForDatabasePlaces(places[0],places[0].location);
			      	}
			      	else{
			      		addSelectedCityMarker(places[0]);
			    	}
			      	}else{
			      			
			      		deletedSelectedCityMarker(places[0]);
			      } 
				 });
		  });
		 
 }
    
    function getSelectedPlaceTypeVal(){
      return  $(" #addNewPlaceTypeSelectBox option:selected").val();
    }
    function addActionToListItems(){
        
		  $("#relatedPlacesList li").each(function(index) {
			 var thisLi=$(this);
			  $(this).click(function() {
				
				  var placeId = thisLi.attr("id");
				  var latitude = thisLi.attr("lat");
				  var longitude = thisLi.attr("lgt");
				  var location = $gmap.createLocation(latitude , longitude);
				  selectOneUserCityHandler(thisLi.find("span").html(),placeId,location);
				 });
			});
}
    function selectOneUserCityHandler(selectedPlace,placeId,location){
    	var oneLocationContent = createLocationContentForInfoWindow(selectedPlace);
    	var container  = addLocationToLocationContainer(oneLocationContent);
        var html = $("<div>").append(container).html();
		var infoWindowContent = html;
		var infoWindowRequest={"requestType":"myPlace","selectedPlaceId":placeId,"selectedPlace":selectedPlace,"location":location,"placeType":currentSelectedPlaceType};
		var place=new Array();
		place[0]=infoWindowRequest;
		$gmap.openInfoWindowOnMap(infoWindowContent,location,infoWindowContentEventHandler,place);	
	}	
    
    function addSelectedCityMarkerForDatabasePlaces(selectedPlace,location){
    	  var selectedPlaceType = getSelectedPlaceTypeVal();
    	 var request = {
    	    		  'searchType':'myPlace',
    	    		  'placeId':selectedPlace.selectedPlaceId,
    	    		  'placeCityId':$appRequest.addPlaceCityId,
    	    		  'placeType':selectedPlaceType
    	           }
    		   	 	
    		   	 var requestString = JSON.stringify(request); 
    		   	 $appRequest.addPlaceToCity(requestString,addCityMarkerHandler);
    			  
    			 //alert(selectedPlace);
    			 createMarkerForCityForPlace(selectedPlace,location);
    		   	 $gmap. closeCurrentInfoWindow();
    }
    
  function addSelectedCityMarker(selectedPlace){
	  var selectedPlaceType = getSelectedPlaceTypeVal();
	  var selectedCityJson = {
   	 	'latitude':selectedPlace.geometry.location.lat(),
   	 	'longitute':selectedPlace.geometry.location.lng(),
   	 	'placeName':selectedPlace.formatted_address
   	 }
   	 
   	 var request = {
	   'searchType':'gmap', 		  
   	   'placeCity':selectedCityJson,
   	   'placeCityId':$appRequest.addPlaceCityId,
   	   'placeType':selectedPlaceType
   	 }
   	 	
   	 var requestString = JSON.stringify(request); 
   	 $appRequest.addPlaceToCity(requestString,addCityMarkerHandler);
	  
	  //alert(selectedPlace.formatted_address);
   	 createMarkerForCity(selectedPlace);
   	 $gmap. closeCurrentInfoWindow();
   }
    function createMarkerForCityForPlace(selectedPlace,location){
    	var image = "";
    	var imageUrl = "../images/defaultMarker.png";
        //var title = selectedPlace.formatted_address.split(",")[0];
        image =  $gmap.createMarkerImage(imageUrl);
        $gmap.addMarkerToMap(location,image);
    	
    } 
   function createMarkerForCity(selectedCity,cityType){
   	   var image = "";
   	   var location = selectedCity.geometry.location;
       var imageUrl = "../images/defaultMarker.png";
       var title = selectedCity.formatted_address.split(",")[0];
       image =  $gmap.createMarkerImage(imageUrl);
       $gmap.addMarkerToMap(location,image,title);
   	
   }
   function initlialize(){
   	    $gmap.setLatitude(-34.397).setLongitude(150.644).mapInit();
		var placeType =  $gmap.placeType;
		var input = document.getElementById("searchTextField");
		var placeTypes = [$gmap.placeType.POLITICAL,$gmap.placeType.LOCALITY];
		$gmap.addAutoCompletePlaceInput(input,autoComplatePlaceHandler,placeTypes);
		addActionToListItems();
		
	}
 
   $(document).ready(function(){
	   initlialize(); 
	   });        
	    
})();